package com.saltedfish.tool.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.saltedfish.tool.R;
import com.saltedfish.tool.model.WeekInfo;
import com.saltedfish.tool.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.popwindow.DialogHelper;
import cn.droidlover.xdroidmvp.util.ToastUtils;
import cn.droidlover.xdroidmvp.view.picker.WheelDateView;

public class UIUpdateTimeDialog extends DialogHelper {

    @BindView(R.id.tvBack)
    TextView tvBack;

    @BindView(R.id.tvDateTitle)
    TextView tvDateTitle;

    @BindView(R.id.tvSave)
    TextView tvSave;

    @BindView(R.id.whellView1)
    WheelDateView whellView1;

    @BindView(R.id.whellView2)
    WheelDateView whellView2;

    @BindView(R.id.whellView3)
    WheelDateView whellView3;

    @BindView(R.id.whellView4)
    WheelDateView whellView4;

    @BindView(R.id.whellView5)
    WheelDateView whellView5;

    @BindView(R.id.whellView6)
    WheelDateView whellView6;

    @BindView(R.id.whellView7)
    WheelDateView whellView7;

    private String repeatTime;

    public UIUpdateTimeDialog(Activity context) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;

        setFullWidthScreen();
        setGravity(Gravity.BOTTOM);
        setAnimSet(R.style.popBottomPopAnim);

    }

    public void updateDate(String repeatTime) {
        this.repeatTime = repeatTime;

        if(!TextUtils.isEmpty(repeatTime)){
            String[] repeatTimes = repeatTime.split("-");
            String startTime[] =  repeatTimes[0].split(":");
            String endTime[] =  repeatTimes[1].split(":");
            whellView1.setItems(date1,startTime[0]);
            whellView3.setItems(date3,startTime[1]);
            whellView5.setItems(date5,endTime[0]);
            whellView7.setItems(date7,endTime[1]);
        }else {
            initView();
        }

    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ui_update_time;
    }

    private List<String> date1;
    private List<String> date2;
    private List<String> date3;
    private List<String> date4;
    private List<String> date5;
    private List<String> date6;
    private List<String> date7;

    private String hour1;
    private String minute1;
    private String hour2;
    private String minute2;

    @Override
    protected void initView() {
        tvDateTitle.setText("修改当天时间");
        date1 = new ArrayList<>();
        date2 = new ArrayList<>();
        date3 = new ArrayList<>();
        date4 = new ArrayList<>();
        date5 = new ArrayList<>();
        date6 = new ArrayList<>();
        date7 = new ArrayList<>();

        date2.add(":");
        date4.add("至");
        date6.add("：");
        for (int i = 0; i <= 23; i++) {
            String index;
            if(i<10){
                index ="0" + i;
            }else{
                index = i +"";
            }
            date1.add(index);
            date5.add(index);
        }

        for (int i = 0; i <= 59; i++) {
            String index;
            if(i<10){
                index ="0" + i;
            }else{
                index = i +"";
            }
            date3.add(index);
            date7.add(index);
        }

        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.getWindow().setStatusBarColor(mContext.getResources().getColor(R.color.color_white));
                String startTime = hour1 + ":" + minute1;
                String endTime = hour2 + ":" + minute2;
                repeatTime = startTime + "-" + endTime;

//                int index = DateUtils.compareEqualsHour(startTime, endTime);
////        XLog.e(subFlagModel.toString()+"-index---"+index);
//                if(index == 1){
//                    repeatTime = startTime + "-" + endTime;
////                }else {
////                    ToastUtils.showMessage(mContext,"开始时间必须小于结束时间");
////                    return;
//                }
                if(listener !=null){
                    listener.onDateClicks(repeatTime);
                }

                dismiss();
            }
        });
        whellView4.setTextColor(0XFF333333);

        whellView1.setItemsTwo(date1);
        whellView3.setItemsTwo(date3);
        whellView5.setItemsTwo(date5);
        whellView7.setItemsTwo(date7);

        whellView2.setItems(date2);
        whellView4.setItems(date4);
        whellView6.setItems(date6);

        whellView1.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                hour1 = item;
            }
        });

        whellView3.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                minute1 = item;
            }
        });
        whellView5.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                hour2 = item;
            }
        });

        whellView7.setOnWheelViewListener(new WheelDateView.OnWheelViewListener() {
            @Override
            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
                minute2 = item;
            }
        });

    }


    List<WeekInfo> selectList;


    @Override
    protected void initData() {
        selectList = new ArrayList<>();
        selectList.add(new WeekInfo("周一",false));
        selectList.add(new WeekInfo("周二",false));
        selectList.add(new WeekInfo("周三",false));
        selectList.add(new WeekInfo("周四",false));
        selectList.add(new WeekInfo("周五",false));
        selectList.add(new WeekInfo("周六",false));
        selectList.add(new WeekInfo("周日",false));

    }


    @Override
    protected void dialogDismiss() {
        dismiss();
    }

    @Override
    public void dismiss() {

        super.dismiss();

    }


    public SimpleDateListener listener;

    public static class SimpleDateListener{
        public void onDateClicks(String time) { }
    }

    public void setListener(SimpleDateListener listener) {
        this.listener = listener;
    }

}
